package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.HashChargeContact;
import calorie.com.calorie.response.TransferAccountResponce;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 输入Hash值
 * Created by ThinkPad on 2018-11-28.
 */

public class InputHashActivity extends MvpActivity<HashChargeContact.HashChargePresent> implements HashChargeContact.IHashChargeView {
    public static final String TYPE = "type";
    @BindView(R.id.et_hash)
    EditText etHash;
    private String type;

    @Override
    public void initData(Bundle savedInstanceState) {
        type= getIntent().getStringExtra(TYPE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_hash;
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String hashCode=etHash.getText().toString();
        if (ObjectUtils.isEmpty(hashCode)){
            ToastUtils.showLongToast(context,"请输入hash值");
            return;
        }
        if (!RegexUtils.isDigitalAndLetter(hashCode)&&hashCode.length()!=66){
            ToastUtils.showLongToast(context,"请输入66位字母加数字的hash值");
            return;
        }
        showLoadingDialog();
        mPresent.hashCharge(context,hashCode,type);
    }

    @Override
    public void getHashChargeData(Response<TransferAccountResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "充币成功");
            StaticValue.userAssets = new Gson().fromJson(new Gson().toJson(response.body().getData()), UserAssets.class);
            finish();
            return;
        }
        ToastUtils.showLongToast(context, response.body().getMsg());
        dismissLoadingDialog();
    }

    @Override
    protected HashChargeContact.HashChargePresent createPresent() {
        return new HashChargeContact.HashChargePresent();
    }
}
